define('controllers/sp_school_screen', function (require, exports, module) {
	"use strict";

	var fnGlobalUrl = require[require.toUrl ? 'toUrl' : 'resolve'];
	var $window = $(window),
		$document = $(document);

	/*var m_Swiper = require('swiper');*/ //引用一个js插件实例

	/* ************************************************************ */
	var m_SchoolScreen = { /* 定义一个首页对象 */
		createNew: function () {
			if(!(window.basePage) || typeof(window.basePage.createNew) !=='function'){
				window.basePage={};
				window.basePage.createNew=function(){
					return {};
				}
			}
			var $internal = window.basePage.createNew();
			/* code here for example */
			$internal.screen = null;

			$internal.init = function () { /* 页面类的初始化， 向$internal上绑定功能对象， 如下绑定 Banner 对象 */
				/*code here 如下： */
				this.screen = new $screen();

			};
			var $screen = new Class({ /* 创建banner对象 */
				initialize: function () {
					this._init();
				}
			});
			$screen.extend({ /* 向banner对象添加 banner的方法  */
				_init: function () {
					this.isAppear = false; /* 是否呈现完毕标志 */
					this.mySwiper = null;
					this.myTimers = undefined; /* 预定义定时器  */
					this.scrNav = $('.J_screen_nav');
					this.scrScreen = $('.J_screen');
					this.scrDelete = $('.J_scrDel');
					this.scrSort = $('.J_sort_list');
					this.schoolList = $('.J_school_list');
				},
				appear: function () { /* 呈现相关逻辑  如swiper 的配置和和绑定等 */
					var _this = this;
					_this.stateSet();
					_this.screenEvent();
				},
				stateSet:function(){
					var _this = this,
					    navName = _this.scrNav.attr('data-typeName'),
							navId = _this.queryString(navName),
							hasScr = 0;
					// 筛选导航状态
					if(navId){
						_this.scrNav.find('.item').each(function(){
							var _the = $(this);
							if(_the.attr('data-typeId')==navId){
								_the.addClass('active');
							}
						});
					}else{
						_this.scrNav.find('.item:eq(0)').addClass('active');
					}

					// 筛选状态
					_this.scrScreen.find('.J_screen_item').each(function(){
						var _the = $(this),
								_theName = _the.attr('data-typeName'),
								_theId = _this.queryString(_theName);
						if(_theId){
							_the.find('.option').each(function(){
								var opt = $(this);
								if(opt.attr('data-typeId')==_theId){
									var newLi = $('<span class="option"></span>');
									newLi.html(opt.text()+'<i></i>');
									newLi.attr('data-typeName',_theName);
									opt.addClass('active');
									_this.scrDelete.show().find('dd').append(newLi);
									hasScr = 1;
								}
							});
						}
					});
					if(!hasScr){
						_this.scrDelete.hide();
					}

					// 列表样式
					var listStyle = _this.queryString('listStyle');
					if(listStyle){
						_this.scrSort.find('.J_list_style:eq(0)').addClass('active');
						_this.schoolList.addClass('typelist').removeClass('typelarge');
					}else{
						_this.scrSort.find('.J_list_style:eq(1)').addClass('active');
					}
					// 排序状态
					var sortName = _this.scrSort.attr('data-sortName'),
							nowSort = _this.queryString(sortName);
					if(nowSort){
						if(nowSort%2==0){
							var _theNum = nowSort/2;
							_this.scrSort.find('.J_sort_li:eq('+_theNum+')').addClass('down');
						}else{
							var _theNum = (nowSort-1)/2;
							_this.scrSort.find('.J_sort_li:eq('+_theNum+')').addClass('up');
						}
					}else{
						_this.scrSort.find('.J_sort_li:eq(0)').addClass('down');
					}
				},
				// 全部事件
				screenEvent:function(){
					// 点击导航
					var _this = this,
							navName = _this.scrNav.attr('data-typeName');
					_this.scrNav.on('click','.item',function(){
						var _the = $(this),
								_theId = _the.attr('data-typeId');
						if(!_the.hasClass('active')&&navName&&_theId){
							var newLink = _this.changeString(navName,_theId);
							window.location.replace(newLink);
						}
					});
					// 点击筛选
					_this.scrScreen.find('.J_screen_item').on('click','.option',function(){
						var _the = $(this),
								_theId = _the.attr('data-typeId'),
								_theName = _the.parents('.J_screen_item').attr('data-typeName');
						if(!_the.hasClass('active')&&_theName&&_theId){
							var newLink = _this.changeString(_theName,_theId);
							window.location.replace(newLink);
						}
					});
					// 删除筛选
					_this.scrDelete.on('click','.option',function(){
						var _the = $(this),
								_theName = _the.attr('data-typeName'),
						    newLink = _this.changeString(_theName,0);
						window.location.replace(newLink);
					});
					// 点击列表样式
					_this.scrSort.on('click','.J_list_style',function(){
						var _the = $(this),
								_theNum = _the.attr('data-num');
						if(!_the.hasClass('active')&&_theNum){
							var newLink = _this.changeString('listStyle',_theNum);
							window.location.replace(newLink);
						}
					});
					// 点击排序
					_this.scrSort.on('click','.J_sort_li',function(){
						var _the = $(this),
								_theNum = (parseInt(_the.index()+1)*2)-2,
								_theName = _this.scrSort.attr('data-sortName'),
								oldNum = _this.queryString(_theName);
						if(_the.hasClass('down')){							
							var newLink = _this.changeString(_theName,_theNum+1);
							window.location.replace(newLink);
						}else{
							var newLink = _this.changeString(_theName,_theNum);
							window.location.replace(newLink);
						}
					});
					// 点击清除筛选
					_this.scrDelete.on('click','.J_allDel',function(){
						var newLink = null;
						_this.scrDelete.find('.option').each(function(){
							var _the = $(this),
									_theName = _the.attr('data-typeName');
							newLink = _this.changeString(_theName,0,newLink);
						});
						window.location.replace(newLink);
					});
				},
				queryString: function(name){
			    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			    var r = window.location.search.substr(1).match(reg);
			    if(r!=null)return  unescape(r[2]); return null;
				},
				changeString: function(name,id,pUrl){
					var _this = this,
							oldId = _this.queryString(name),
							newType = name + '=' + id,
							oldType = name + '=' + oldId,
							oldLink = pUrl||window.location.href,
							hasType = oldLink.indexOf("&")==-1,
							newLink = null,
							cleanPage = [],
							oldPage = _this.queryString('page');
					// 获取需要清除分页name
					if(oldPage){
						cleanPage.push(_this.scrNav.attr('data-typeName'));
						for(var i=0;i<_this.scrScreen.find('.J_screen_item').length;i++){
							cleanPage.push(_this.scrScreen.find('.J_screen_item:eq('+i+')').attr('data-typeName'));
						}
						cleanPage.push(_this.scrSort.attr('data-sortName'));

						for(var i=0;i<cleanPage.length;i++){
							if(name==cleanPage[i]){
								var _thePage = 'page=';
								if(oldLink.indexOf('&page='+oldPage)==-1){
									if(oldLink.indexOf('page='+oldPage+'&')==-1){
										_thePage = '?' + _thePage + oldPage;
									}else{
										_thePage = _thePage + oldPage + '&';
									}
								}else{
									_thePage = '&' + _thePage + oldPage;
								}
								oldLink = oldLink.replace(_thePage,'');
							}
						}
					}

					if(id==0){
						newType = '';
						if(oldLink.indexOf('&'+oldType)==-1){
							if(oldLink.indexOf(oldType+'&')==-1){
								oldType = '?'+oldType;
							}else{
								oldType = oldType+'&';
							}
						}else{
							oldType = '&'+oldType;
						}
					}
					if(oldId){
						newLink = oldLink.replace(oldType,newType);
					}else{
						if(!(oldLink.indexOf("?")==-1)){
							newLink = oldLink + '&' + newType;
						}else{
							newLink = oldLink + '?' + newType;
						}
					}

					return newLink
				} 
			});
			return $internal; //返回对象
		}


	};
	exports.run = function (){
		var SCHOOLSCREEN = m_SchoolScreen.createNew(); /* 定义首页页面类 */
		SCHOOLSCREEN.init(); /* 调用首页页面类入口方法  */
		$document.ready(function () {
			SCHOOLSCREEN.screen.appear(); /* 当dom 加载完成后调用 banner 的呈现函数 */
		});
	};
});
